﻿/*
BF3Rcon.NET, provides a .NET implementation of BF3's RCON interface.
Copyright (C) 2011 Timi

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

You can contact us at http://bf3rcon.codeplex.com/.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.Net.Battlefield3
{
    /// <summary>
    /// Contains members that represent the successfulness of certain requests.
    /// </summary>
    public class RconResult
    {
        Packet Packet;

        /// <summary>
        /// Shows whether or not the request was successful.
        /// </summary>
        /// <value>True if the request didn't time out and the first word is "OK".</value>
        /// <remarks>
        /// If <see cref="Success"/> is false and and <see cref="Error"/> is null (Nothing in Visual Basic), then the request "failed"
        /// due to timeout. It is still possible the request was successful, but a response wasn't received by the time determined by
        /// <see cref="RconClientBase.SynchronousReadTimeout"/>.
        /// </remarks>
        public bool Success
        {
            get { return Packet.Success(); }
        }

        /// <summary>
        /// Shows the error that occured when the request fails.
        /// </summary>
        /// <value>The first string of a <see cref="Packet"/> when <see cref="Success"/> is false; null if <see cref="Success"/> is false
        /// due to timeout; null if <see cref="Success"/> is true.</value>
        /// <remarks>
        /// If <see cref="Success"/> is false and and <see cref="Error"/> is null (Nothing in Visual Basic), then the request "failed"
        /// due to timeout. It is still possible the request was successful, but a response wasn't received by the time determined by
        /// <see cref="RconClientBase.SynchronousReadTimeout"/>.
        /// </remarks>
        public string Error
        {
            get { return Success || (Packet != null && Packet.Words.Count == 0) ? null : Packet.Words[0]; }
        }

        //only the word matters
        static readonly Packet SucessPacket = new Packet(false, false, 0, "OK");
        internal static RconResult SuccessfulResult
        {
            get { return SucessPacket.ToResult(); }
        }

        static readonly Packet FailedPacket = new Packet(false, false, 0, "NotOK");
        internal static RconResult FailedResult
        {
            get { return FailedPacket.ToResult(); }
        }

        /// <summary>
        /// Defines an implicit conversion of an <see cref="RconResult"/> to a boolean value.
        /// </summary>
        /// <param name="result">The <see cref="RconResult"/> to convert to a boolean.</param>
        /// <returns>The equivalent of <see cref="Success"/>, where true represents a successful request.</returns>
        public static implicit operator bool(RconResult result)
        {
            return result.ToBoolean();
        }

        /// <summary>
        /// Converts the <see cref="RconResult"/> into a boolean value, which is equal to <see cref="Success"/>.
        /// </summary>
        /// <returns>The same value as <see cref="Success"/>, which is true if the request was successful.</returns>
        public bool ToBoolean()
        {
            return Success;
        }

        ///// <summary>
        ///// Defines an implicit conversion of an <see cref="RconResult"/> to a string.
        ///// </summary>
        ///// <param name="result">The <see cref="RconResult"/> to convert to a string.</param>
        ///// <returns>The equivalent of <see cref="Error"/>.</returns>
        //public static implicit operator string(RconResult result)
        //{
        //    return result.Error;
        //}

        internal RconResult(Packet packet)
        {
            Packet = packet;
        }
    }
}
